<template>
  <!--案件导入-案件上传导入-->
  <section class="page introduction-case">
    <data-form :model="introductionModel" @onSearch="refreshData" :rules="introductionRule" :page="pageService" @handleResetForm="follFeedback">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="introductionModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="introductionModel.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="introductionModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="introductionModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="逾期天数" prop="dayRange">
          <number-range v-model="introductionModel.dayRange" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="逾期总金额" prop="amountRange">
          <number-range v-model="introductionModel.amountRange"></number-range>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="introductionModel.principalId" placeholder="请选择" clearable>
            <el-option v-for="item in principalList.content" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="introductionDataSet" @onPageChange="refreshData" :page="pageService">
      <template slot="buttons">
        <!--按钮区域-->
        <el-button @click="dialog.caseExcelImport = true" v-auth = "106">案件导入</el-button>
        <el-button :disabled="!isImported" @click="dialog.confirmBatch = true" v-auth = "107">案件确认</el-button>
        <el-button :disabled="!isImported" @click="dialog.deleteBatch = true" v-auth = "108">案件删除</el-button>
      </template>
      <template slot="columns">
        <el-table-column prop="caseInfo.caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueAmount" label="逾期金额(元)" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" :min-width="$helper.getColumnWidth(5)" :formatter="(row) => $filter.dateTimeFormat(row.createTime)">
        </el-table-column>
      </template>
    </data-box>

    <!--案件导入弹出框-->
    <el-dialog title="案件导入" :visible.sync="dialog.caseExcelImport" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <case-excel-import ref="case-excel-import" @close="dialog.caseExcelImport = false;refreshData();" @success="refreshData"></case-excel-import>
    </el-dialog>
    <!--选择案件批次-->
    <el-dialog title="选择案件批次" :visible.sync="dialog.confirmBatch" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <case-batch-confirm :batchList="batchList" @close="dialog.confirmBatch = false;refreshData()" @success="refreshData"> </case-batch-confirm>
    </el-dialog>
    <!--案件删除弹出框-->
    <el-dialog title="案件删除" :visible.sync="dialog.deleteBatch" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <case-batch-delete :batchList="batchList" @close="dialog.deleteBatch = false;refreshData()" @success="refreshData"></case-batch-delete>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataForm from '~/components/common/data-form.vue';
import DataBox from '~/components/common/data-box.vue';
import { State, Action, namespace, Getter } from 'vuex-class';
import NumberRange from '~/components/common/number-range.vue';
import CaseExcelImport from '~/components/case-import/case-excel-import.vue';
import CaseBatchConfirm from "~/components/case-import/case-batch-confirm.vue";
import CaseBatchDelete from "~/components/case-import/case-batch-delete.vue";
import { PageService } from '~/utils/page.service';
import { CaseInfoService } from '~/services/domain-service/case-info.service';
import { DataImportExcelInfoService } from '~/services/dataimp-service/data-import-excel-info.service';
import { CommonService } from '~/utils/common.service';
import { OpenExecutionData } from "~/config/types";
import { CASE_CHANNEL } from '~/config/enum.config';
import { ReminderService,ReminderMessageType } from "~/utils/reminder.service";

const CollectionExecutionModule = namespace('collection-execution');

@Layout('workspace')
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    CaseExcelImport,
    CaseBatchConfirm,
    CaseBatchDelete
  }
})
export default class IntroductionCase extends Vue {
  @State private principalList: any;
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(CaseInfoService) private caseInfoService: CaseInfoService;
  @Dependencies(DataImportExcelInfoService)
  private importExcelService: DataImportExcelInfoService;
  @CollectionExecutionModule.Action private openCollectionExecution;

  private dialog: any = {
    caseExcelImport: false,
    confirmBatch: false,
    deleteBatch: false,
  };

  private batchList:any = []

  private caseDetail: any = '';
  private introductionModel: any = {
    batchNumber: '',
    caseNumber:'',
    mobileNo: '',
    personalName: '',
    dayRange: { min: '', max: '' },
    amountRange: { min: '', max: '' },
    principalId: '',
  };
  private deleteImport: any = {
    batchNumber: '',
  };
  private introductionDataSet: any[] = [];
  private introductionRule = {};

  //判断是否有批次号
  get isImported() {
    return this.batchList.length > 0;
  }

  private steplength: number = 0

  //获取案件列表
  public refreshData() {
    this.importExcelService.getAllImportExcelInfos(
      this.introductionModel,
      this.pageService,
    ).subscribe(
      (data) => {
        this.introductionDataSet = data;
        if(this.introductionDataSet){
          this.introductionDataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      }
    );
    this.updateBatchList()
  }

  private mounted() {
    this.refreshData();

    // 注册消息监听,用于业务自动刷新
    // 刷新数据列表
    ReminderService.getInstance()
      .addListener(ReminderMessageType.Background,"IMPORT_EXCEL")
      .subscribe(this.refreshData)
    // 更新每个批次详情，用于在案件确认时
    ReminderService.getInstance()
      .addListener(ReminderMessageType.Background,"CONFIRM_CASE")
      .subscribe(this.updateBatchList)
  }

  /**
   * 更新批次数据
   */
  private updateBatchList(){
     this.importExcelService.getAllBatchNumbers()
      .subscribe(data => {
          this.batchList = data
      })
  }
  

  /**
   * 重置numberRange
   */
  private follFeedback() {
    this.introductionModel.dayRange = { min: '', max: '' };
    this.introductionModel.amountRange = { min: '', max: '' };
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(new OpenExecutionData(row.id, row.caseNumber, CASE_CHANNEL.import, true,true))
  }
}
</script>

<style lang="less">
.page.introduction-case {
  .data-box {
    .error-row > td {
      color: #ff9813 !important;
      span {
        color: #ff9813 !important;
      }
    }
  }
}
</style>
